//
//  HomeViewController.m
//  JY_iOS
//
//  Created by whlx on 2018/11/26.
//  Copyright © 2018年 TxY. All rights reserved.
//

#import "HomeViewController.h"
#import "HomeCollectionViewCell.h"
#import "HomeCellHeadView.h"
#import "SDCycleScrollView.h"
//#import "VideoViewController.h"
#import <MediaPlayer/MediaPlayer.h>
//#import "bannerModel.h"
//#import "moiveModel.h"
//#import "serchViewController.h"
#import "HomeTableViewCell.h"
#import "newEditViewController.h"
#import "JQFlowView.h"
#import "JQIndexBannerSubview.h"

#import "UserListViewController.h"
#define BgColor [UIColor colorWithRed:242/255.0 green:243/255.0 blue:248/255.0 alpha:1]
@interface HomeViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,UITableViewDelegate,UITableViewDataSource,SDCycleScrollViewDelegate,JQFlowViewDelegate,JQFlowViewDataSource>{
    
    UICollectionView *_myCollectionView;
    
    UITableView *  _datatableView;
    NSArray *_listArray;
    NSMutableArray <NSArray*>*_dataArray;
    SDCycleScrollView *_ImageListView;
    NSArray *_ImageDataArray;

//    ///免费
//    NSArray *_freeDataArray;
//    ///最新
//    NSArray *_newDataArray;
//    ///最热
//    NSArray *_hotDataArray;
    
    
}




@end

@implementation HomeViewController

static NSString *ItemIdentifier = @"ItemIdentifier";

static NSString *leaveDetailsHeadID = @"leaveDetailsHeadID";

static NSString *leaveDetailsFooterID = @"leaveDetailsFooterID";


- (void)viewDidLoad {
    [super viewDidLoad];
//    self.view.backgroundColor=[UIColor redColor];
    // Do any additional setup after loading the view from its nib.
    self.view.backgroundColor = [UIColor blackColor];

    [self addNavigationItemWithImageNames:@[@"我的"] isLeft:YES target:self action:@selector(didClickLeft) tags:nil];
    
    
    _listArray = [NSArray arrayWithObjects:@"免费课程",@"最新课程",@"热门课程", nil];
    _dataArray=@[@[],@[],@[]].mutableCopy;
//    dataArray = [NSArray arrayWithObjects:@"11",@"12",@"13",@"14" ,nil];


    [self createCollectionView];
    [self setHederTableView];
//  [self getImageList];
    self.navigationItem.title = @"";
    
   
}
-(void)didClickLeft{
    UserListViewController *vc = [UserListViewController new];
    [self cw_showDefaultDrawerViewController:vc];
}
-(void)setHederTableView{
    _datatableView = [[UITableView alloc]initWithFrame:CGRectMake(0,-910, [UIScreen mainScreen].bounds.size.width, 910) style:UITableViewStylePlain];
    _datatableView.delegate = self;
    _datatableView.dataSource = self;
    _datatableView.backgroundColor = [UIColor clearColor];
    _datatableView.separatorStyle = UITableViewCellEditingStyleNone;
    //    self.tableView  = tableView;
//    [self.view addSubview: _datatableView];
    
    [_datatableView registerNib: [UINib nibWithNibName:@"HomeTableViewCell" bundle:nil] forCellReuseIdentifier:@"HomeTableViewCell"];
    
    [self setHeaderView];
    
    
    [_myCollectionView addSubview:_datatableView];
    
}
-(void)didClickTopBtn{
//    serchViewController *search = [serchViewController new];
//    [self.navigationController pushViewController:search animated:YES];
}

-(void)createCollectionView{
    
    
    UICollectionViewFlowLayout *lay = [[UICollectionViewFlowLayout alloc]init];
    //    layout.minimumLineSpacing = 40;
    
    CGFloat cellSpace = 0.0;
    CGFloat cellWidth = KScreenWidth/2.0;
    
    lay.itemSize = CGSizeMake(cellWidth, 130);//Item size(每个item的大小)
    
    lay.sectionInset = UIEdgeInsetsMake(cellSpace, cellSpace, cellSpace, cellSpace);//某个section中cell的边界范围。
    
        lay.headerReferenceSize = CGSizeMake(KScreenWidth, 40);//每个section的Header宽高
        lay.footerReferenceSize = CGSizeMake(KScreenWidth, 0.1);//每个section的Footer宽高
    
    lay.minimumInteritemSpacing = cellSpace;//Inter cell spacing（每行内部cell item的间距）
    lay.minimumLineSpacing = cellSpace;//Line spacing（每行的间距）
    //横向滑动
    lay.scrollDirection = UICollectionViewScrollDirectionVertical;
    
//    910
    
    _myCollectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, KScreenHeight- kTopHeight-kTabBarHeight) collectionViewLayout:lay];
    _myCollectionView.delegate = self;
    _myCollectionView.dataSource = self;
    
    _myCollectionView.contentInset = UIEdgeInsetsMake(910, 0, 0, 0);
    //    collectionView.backgroundColor = [UIColor purpleColor];
//    [self.contentView addSubview:collectionView];
    
    [_myCollectionView registerNib: [UINib nibWithNibName:@"HomeCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"HomeCollectionViewCell"];
    
    [_myCollectionView registerNib:[UINib nibWithNibName:@"HomeCellHeadView" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:leaveDetailsHeadID];
    
    //一定要注册footerview
    [_myCollectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:leaveDetailsFooterID];
    
    [self.view addSubview:_myCollectionView];
    
    
    [_myCollectionView scrollToTop];
}

///设置轮播图
 - (void)setHeaderView {
   

     UIView *headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 230)];
     
     UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(20, 0, kScreenWidth-40, 40)];
     [btn setTitle:@"输入关键字" forState:UIControlStateNormal];
     [btn setTitleColor:CHex(@"#444444") forState:UIControlStateNormal];
     btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
     [headerView addSubview:btn];
     
     UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(20, CGRectGetMaxY(btn.frame)+1, kScreenWidth-40, 1)];
     lineView.backgroundColor = CLineColor;
     [headerView addSubview:lineView];
     
     
     
     
    JQFlowView* _pageFlowView = [[JQFlowView alloc] initWithFrame:CGRectMake(0, 60, KScreenWidth, 170)];
     
     _pageFlowView.backgroundColor = [UIColor clearColor];
     _pageFlowView.delegate = self;
     _pageFlowView.dataSource = self;
     _pageFlowView.minimumPageAlpha = 0.1;
     _pageFlowView.minimumPageScale = 0.97;
     _pageFlowView.orginPageCount = 5;
     _pageFlowView.isOpenAutoScroll = YES;
     _pageFlowView.autoTime = 3.0;
     _pageFlowView.orientation = JQFlowViewOrientationHorizontal;
     //初始化pageControl
     UIPageControl *pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, _pageFlowView.frame.size.height - 24 - 8, KScreenWidth, 8)];
     _pageFlowView.pageControl = pageControl;
     [_pageFlowView addSubview:pageControl];
     
     [_pageFlowView reloadData];
     [headerView addSubview:_pageFlowView];
     
     
//   _ImageListView =  [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 60, kScreenWidth, 170) delegate:self placeholderImage:nil];
//     _ImageListView.pageControlAliment =SDCycleScrollViewPageContolAlimentCenter;
//     _ImageListView.pageControlStyle = SDCycleScrollViewPageContolStyleAnimated;
//
//     [headerView addSubview:_ImageListView]; _ImageListView.localizationImageNamesGroup=@[@"guideImage1",@"guideImage2",@"guideImage3"];
     
     _datatableView.tableHeaderView = headerView;
//   [_datatableView :_ImageListView];
     
     
 }

#pragma mark --轮播图代理
- (CGSize)sizeForPageInFlowView:(JQFlowView *)flowView
{
    return CGSizeMake(KScreenWidth - 40, 170);
}
- (void)didSelectCell:(UIView *)subView withSubViewIndex:(NSInteger)subIndex
{
    NSLog(@"点击了第%ld张图",(long)subIndex + 1);
}

- (NSInteger)numberOfPagesInFlowView:(JQFlowView *)flowView
{
    return 5;
}
- (UIView *)flowView:(JQFlowView *)flowView cellForPageAtIndex:(NSInteger)index
{
    JQIndexBannerSubview *bannerView = (JQIndexBannerSubview *)[flowView dequeueReusableCell];
    if (!bannerView) {
        bannerView = [[JQIndexBannerSubview alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth - 40, 170)];
        bannerView.layer.cornerRadius = 4;
        bannerView.layer.masksToBounds = YES;
        //        bannerView.mainImageView.image = [bannerView.mainImageView.image stretchableImageWithLeftCapWidth:30 topCapHeight:30];
    }
    
    //在这里下载网络图片
//    [bannerView.mainImageView sd_setImageWithURL:[NSURL URLWithString:self.imageArray[index]] placeholderImage:nil];
    
        bannerView.mainImageView.image = [UIImage imageNamed:@"guideImage1"];
    
    return bannerView;
}

- (void)didScrollToPage:(NSInteger)pageNumber inFlowView:(JQFlowView *)flowView
{
    NSLog(@"滚动到了第%ld页",pageNumber);
}


/** 点击图片回调 */
- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index{
    
    
}


#pragma mark --collectionViewDataSoure&&delegate

//cell的header与footer的显示内容
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    
    if (kind == UICollectionElementKindSectionHeader)
    {
        
        HomeCellHeadView* reusableHeaderView = (HomeCellHeadView*)[collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader  withReuseIdentifier:leaveDetailsHeadID forIndexPath:indexPath];
        
        reusableHeaderView.titleText.text = _listArray[indexPath.section];
        reusableHeaderView.didClickHeadr = ^{
            kAppDelegate.mainTabBar.selectedIndex = 1;
        };
        
        return reusableHeaderView;
        
    }else if (kind == UICollectionElementKindSectionFooter){
        
        UICollectionReusableView *reusableFooterView = nil;
        
        if (reusableFooterView == nil) {
            
            reusableFooterView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter  withReuseIdentifier:leaveDetailsFooterID forIndexPath:indexPath];
        }
        
        return reusableFooterView;
    }
    return nil;
    
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return 6;
}


-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    HomeCollectionViewCell *cell =[collectionView dequeueReusableCellWithReuseIdentifier:@"HomeCollectionViewCell" forIndexPath:indexPath];
    
    cell.backgroundColor = [UIColor clearColor];
    //    cell.imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%ld.jpg",indexPath.row+1]];
    //    cell.label.text =[NSString stringWithFormat:@"%zd. %@",indexPath.row+1,self.dataSource[indexPath.row]];
    return cell;
}


#pragma mark --tableViewDataSoure&&delegate

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 170;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 4;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    HomeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HomeTableViewCell"];
//    cell.backgroundColor = [UIColor blueColor];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.didClickItem = ^{
        NSString *filePath = [[NSBundle mainBundle] pathForResource:@"test"ofType:@"mp4"];
        
        newEditViewController *edit = [newEditViewController new];
        edit.editMode =NvEditMode16v9;
        edit.selectPath = @[filePath].mutableCopy;
        
        [self.navigationController pushViewController:edit animated:YES];
    };
    return cell;
}

@end
